package sv.com.renacempleo.dao.ctg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.renacempleo.dao.BaseDaoImpl;
import sv.com.renacempleo.dto.ctg.CtgDepartamento;
import sv.com.renacempleo.exception.WebException;

public class CtgDepartamentoDaoImpl extends BaseDaoImpl implements CtgDepartamentoDao{

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActivesAsArray() throws WebException {
		DetachedCriteria criteria  = DetachedCriteria.forClass(CtgDepartamento.class);
		criteria.addOrder(Order.asc("ctgPais.ctgCatalogoId"));
		criteria.addOrder(Order.asc("ctgDepartamentoNombre"));
		criteria.add(Restrictions.eq("ctgDepartamentoActivo", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDepartamentoId")).
				add(Projections.property("ctgDepartamentoNombre")).
				add(Projections.property("ctgPais.ctgCatalogoId"))
		);
	
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllAsArray() throws WebException {
		DetachedCriteria criteria  = DetachedCriteria.forClass(CtgDepartamento.class);
		criteria.addOrder(Order.asc("ctgPais.ctgCatalogoId"));
		criteria.createAlias("ctgPais", "ctgPais");
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDepartamentoId")).
				add(Projections.property("ctgDepartamentoNombre")).
				add(Projections.property("ctgDepartamentoActivo")).
				add(Projections.property("ctgPais.ctgCatalogoId")).
				add(Projections.property("ctgPais.ctgCatalogoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	public void delete(Long ctgDepartamentoId) throws WebException {
		try {
			String hqlQuery = "delete from CtgDepartamento where ctgDepartamentoId =:ctgDepartamentoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgDepartamentoId", ctgDepartamentoId);
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (Exception e) {
			throw new WebException("Problemas al tratar de eliminar el registro <br />Registro puede estar asociado a un Cliente, Poliza, etc");
		}
	}

	public void toggleActive(Long ctgDepartamentoId,String ctgDepartamentoActivo) throws WebException {
		try {
			String hqlQuery = "update CtgDepartamento set ctgDepartamentoActivo = :ctgDepartamentoActivo where ctgDepartamentoId = :ctgDepartamentoId";
			Map<String,Object> parameters = new HashMap<String, Object>();
			parameters.put("ctgDepartamentoId", ctgDepartamentoId);
			parameters.put("ctgDepartamentoActivo", "1".equals(ctgDepartamentoActivo) ? "0" : "1");
			executeNonHibernateQuery(hqlQuery, parameters);
		} catch (WebException e) {
			throw e;
		} catch (Exception e) {
			throw new WebException("Problemas al actualizar la informaci\u00F3n");
		}
	}

	@SuppressWarnings("unchecked") 
	public List<Object[]> findDepByContry(Long ctgPaisId) throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgDepartamento.class);
		criteria.addOrder(Order.asc("ctgPais.ctgCatalogoId"));
		criteria.addOrder(Order.asc("ctgDepartamentoNombre"));
		criteria.createAlias("ctgPais", "ctgPais");
		criteria.add(Restrictions.eq("ctgPais.ctgCatalogoId", ctgPaisId));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgDepartamentoId")).
				add(Projections.property("ctgDepartamentoNombre")).
				add(Projections.property("ctgDepartamentoActivo")).
				add(Projections.property("ctgPais.ctgCatalogoId")).
				add(Projections.property("ctgPais.ctgCatalogoNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

}
